home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / devel / lisp / akcl_lin.z / akcl_lin / c / run_process.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-08  |  6.7 KB  |  309 lines

  1. /* By Mike Ballantyne */
  2.  
  3.  
  4. #include "include.h"
  5. #ifdef RUN_PROCESS
  6.  
  7. /*
  8.  * System Include Files
  9.  *
  10.  * The system files here each define some part of the information needed to
  11.  * compile the inet package.  They need to exist of every host you port this
  12.  * code to.  I have added some comments that I hope will help you "find"
  13.  * the file if it does not have the same name of your host.
  14.  */
  15. #include <errno.h>    /* errno global, error codes for UNIX IO    */
  16. #include <sys/types.h>    /* Data types definitions            */
  17. #include <sys/socket.h>    /* Socket definitions with out this forget it    */
  18. #include <netinet/in.h>    /* Internet address definition AF_INET etc...    */
  19. #include <signal.h>    /* UNIX Signal codes                */
  20. #include <sys/ioctl.h>    /* IO control standard UNIx fair        */
  21. #include <sys/file.h>
  22. #include <fcntl.h>    /* Function to set socket aync/interrupt    */
  23. #include <sys/time.h>    /* Time for select time out                     */
  24. #include <netdb.h>    /* Data Base interface for network files    */
  25. #include <stdio.h>
  26.  
  27. /* LISP - Lisp Wrapper for the "c" code.
  28.  *
  29.  * The lisp OBJECT is passed to the code and a string must be extracted
  30.  * and null terminated to make it work with the "C" code.
  31.  *
  32.  * Lisp Interface code.
  33.  */
  34.  
  35. static char *lisp_to_string(string)
  36. object string;
  37. {
  38.     int    i, len;
  39.     char    *sself;
  40.     char    *cstr;
  41.  
  42.     len = string->st.st_fillp;
  43.  
  44.     cstr = (char *) malloc (len+1);
  45.     sself = &(string->st.st_self[0]);
  46.     for (i=0; i<len; i++)
  47.     {
  48.         cstr[i] = sself[i];
  49.     }
  50.     cstr[i] = 0;
  51.     return (cstr);
  52. }
  53.  
  54. /* open - Open a socket to a server that you know by port number.
  55.  *
  56.  * The caller must know the number of the service and and name of the
  57.  * host that tyhe serive is on.  The name of the host can be "localhost"
  58.  * for a service on the same host as the clinet.
  59.  *
  60.  */
  61. static int open(host,server)
  62. char    *host;
  63. int    server;
  64. {
  65.     int res;
  66.     int pid;
  67.     int    sock;
  68.     struct    hostent    *hp;
  69.     struct    sockaddr_in    sock_add;    /* Address of socket          */
  70.  
  71.     if((hp = gethostbyname(host)) == NULL)
  72.     {
  73.         FEerror("No such host.");
  74.     }
  75.  
  76.     bzero((char *)&sock_add, sizeof(sock_add));
  77.     bcopy(hp->h_addr, (char *)&sock_add.sin_addr, hp->h_length);
  78.     sock_add.sin_family = hp->h_addrtype;
  79.     sock_add.sin_port = server;
  80.  
  81.     sock = socket( hp->h_addrtype, SOCK_STREAM , 0);
  82.  
  83.     if(sock < 1)
  84.     {
  85.         FEerror("No Sockets!");
  86.     }
  87.  
  88.     if(connect(sock, (char *)&sock_add, sizeof(sock_add)) < 0)
  89.     {
  90.         close(sock);
  91.         FEerror("Connection Failed.");
  92.     }
  93.     pid = getpid();
  94.     if(ioctl(sock, SIOCSPGRP, (char *)&pid) < 0 )
  95.     {
  96.         FEerror("Could not set process group of socket.");
  97.     }
  98.  
  99. #ifdef OVM_IO
  100.     res = fcntl(sock,F_SETFL,FASYNC | FNDELAY);
  101. #else
  102.     res = fcntl(sock,F_SETFL,FASYNC);
  103. #endif
  104.     return(sock);
  105. }
  106. #define SETBUF(stream) \
  107.     stream->sm.sm_buffer = alloc_contblock(BUFSIZ); \
  108.     setbuf(stream->sm.sm_fp,stream->sm.sm_buffer)
  109.  
  110. object make_stream(host_l,socket,smm)
  111. object    host_l;
  112. int socket;
  113. enum smmode smm;
  114. {
  115.     char    *mode;
  116.     object    stream;
  117.     FILE    *fp;
  118.     vs_mark;
  119.  
  120.  
  121.     switch(smm)
  122.     {
  123.     case smm_input:
  124.         mode = "r";
  125.         break;
  126.     case smm_output:
  127.         mode = "w";
  128.         break;
  129.     default:
  130.         FEerror("make_stream : wrong mode");
  131.     }
  132.  
  133.     fp = fdopen(socket,mode);
  134.     stream = (object)  alloc_object(t_stream);
  135.     stream->sm.sm_mode = (short)smm;
  136.     stream->sm.sm_fp = fp;
  137. #ifndef linux
  138.     fp->_base = BASEFF;
  139. #endif
  140.     stream->sm.sm_object0 = Sstring_char;
  141.     stream->sm.sm_object1 = host_l;
  142.     stream->sm.sm_int0 = stream->sm.sm_int1 = 0;
  143.     vs_push(stream);
  144.     SETBUF(stream)  ;
  145.     vs_reset;
  146.     return(stream);
  147. }
  148.  
  149. object make_socket_stream(host_l,port)
  150. object    host_l;
  151. object    port;
  152. {
  153.     char    *host = lisp_to_string(host_l);
  154.     object    stream_in;
  155.     object    stream_out;
  156.     object    stream;
  157.     int    socket;
  158.  
  159.     socket = open(host, fix(port));
  160.        stream_in  = make_stream(host_l,socket, smm_input);
  161.        stream_out = make_stream(host_l,socket, smm_output);
  162.  
  163.     stream = make_two_way_stream(stream_in,stream_out);
  164.  
  165.     return(stream);
  166. }
  167.  
  168. void
  169. siLmake_socket_stream()
  170. {
  171.   check_arg(2);
  172.   vs_base[0] = make_socket_stream(vs_base[0], vs_base[1]);
  173.   vs_pop;
  174. }
  175.  
  176. /*
  177.  * make 2 two-way streams
  178.  */
  179.  
  180. object
  181. make_socket_pair()
  182. {
  183.   int sockets_in[2];
  184.   int sockets_out[2];
  185.   FILE *fp1, *fp2;
  186.   int pid;
  187.   object stream_in, stream_out, stream;
  188.  
  189.   if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockets_in) < 0)
  190.     FEerror("Failure to open socket stream pair", 0);
  191.   if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockets_out) < 0)
  192.     FEerror("Failure to open socket stream pair", 0);
  193.   fp1 = fdopen(sockets_in[0], "r");
  194.   fp2 = fdopen(sockets_out[0], "w");
  195.  
  196. #ifdef OVM_IO
  197.   pid = getpid();
  198.   ioctl(sockets_in[0], SIOCSPGRP, (char *)&pid);
  199.   if( fcntl(sockets_in[0], F_SETFL, FASYNC | FNDELAY) == -1)
  200.     perror("Couldn't control socket");
  201.  
  202. #endif
  203. #ifndef linux
  204.   fp2->_base = BASEFF;
  205.   fp1->_base = BASEFF;
  206. #endif
  207.   stream_in = (object) alloc_object(t_stream);
  208.   stream_in->sm.sm_mode = smm_input;
  209.   stream_in->sm.sm_fp = fp1;
  210.   stream_in->sm.sm_int0 = sockets_in[1];
  211.   stream_in->sm.sm_int1 = 0;
  212.   stream_out = (object) alloc_object(t_stream);
  213.   stream_out->sm.sm_mode = smm_output;
  214.   stream_out->sm.sm_fp = fp2;
  215.   SETBUF(stream_in);
  216.   SETBUF(stream_out);
  217.   stream_out->sm.sm_int0 = sockets_out[1];
  218.   stream_out->sm.sm_int1 = 0;
  219.   stream = make_two_way_stream(stream_in, stream_out);
  220.   return(stream);
  221. }
  222. /* the routines for spawning off a process with streams 
  223.  *
  224.  * Assumes that istream and ostream are both associated
  225.  * with "C" type streams.
  226.  */
  227.  
  228.  
  229. spawn_process_with_streams(istream, ostream, pname, argv)
  230. object istream;
  231. object ostream;
  232. char *pname;
  233. char **argv;
  234. {
  235.  
  236.   int fdin;
  237.   int fdout;
  238.   if (istream->sm.sm_fp == NULL || ostream->sm.sm_fp == NULL)
  239.     FEerror("Cannot spawn process with given stream", 0);
  240.   fdin = istream->sm.sm_int0;
  241.   fdout = ostream->sm.sm_int0;
  242.   if (fork() == 0)
  243.     { /* the child --- replace standard in and out with descriptors given */
  244.       close(0);
  245.       dup(fdin);
  246.       close(1);
  247.       dup(fdout);
  248.       fprintf(stderr, "\n***** Spawning process %s ", pname);
  249.       if (execvp(pname, argv) == -1)
  250.     {
  251.       fprintf(stderr, "\n***** Error in process spawning *******");
  252.       fflush(stderr);
  253.       exit(1);
  254.     }
  255.     }
  256.  
  257.  
  258.  
  259.   
  260. }
  261.     
  262.       
  263. run_process(filename, argv)
  264. char *filename;
  265. char **argv;
  266. {
  267.   object stream = make_socket_pair();
  268.   spawn_process_with_streams(stream->sm.sm_object1,
  269.                 stream->sm.sm_object0,
  270.                 filename, argv);
  271.   vs_base[0] = stream;
  272.   vs_base[1] = Cnil;
  273.   vs_top = vs_base + 2;
  274. }
  275.     
  276. siLrun_process()
  277. {
  278.   int i;
  279.   object arglist;
  280.   char *argv[100];
  281.  
  282.   arglist = vs_base[1];
  283.   argv[0] = "";
  284.   for(i = 1; arglist != Cnil; i++) {
  285.      argv[i] = lisp_to_string(arglist->c.c_car);
  286.      arglist = arglist->c.c_cdr;
  287.   }
  288.   argv[i] = (char *)0;
  289.   run_process(object_to_string(vs_base[0]), argv);
  290. }
  291.  
  292. siLmake_socket_pair()
  293. {
  294.   make_socket_pair();
  295. }
  296.  
  297. init_socket_function()
  298. {
  299.   make_si_function("MAKE-SOCKET-STREAM", siLmake_socket_stream); 
  300.   make_si_function("MAKE-SOCKET-PAIR", siLmake_socket_pair);
  301.   make_si_function("RUN-PROCESS", siLrun_process);
  302. }
  303.  
  304. #else /* no RUN_PROCESS */
  305.  
  306. init_socket_function(){;}
  307.  
  308. #endif     
  309.